package com.zijiebutiao.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijiebutiao.pojos.*;
import com.zijiebutiao.service.TCheckgroupCheckitemService;
import com.zijiebutiao.service.TCheckgroupService;
import com.zijiebutiao.service.TCheckitemService;
import com.zijiebutiao.service.TSetmealCheckgroupService;
import com.zijiebutiao.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * @Date 2023/2/28 20:07
 * @Author 干饭
 * @Email 1229219472@qq.com
 * @Version v1.0
 */
@RestController
@RequestMapping("/checkgroup")
@Api(tags = "检查组管理")
public class TCheckgroupController {
    @Autowired
    private TCheckgroupService tCheckgroupService;
    @Autowired
    private TCheckgroupCheckitemService service;
    @Autowired
    private TSetmealCheckgroupService tservice;

    @PostMapping("/findPage.do")
    @ApiOperation(value = "分页查询", notes = "传递pararm对象其中查询条件可以为空")
    public ResultVo getAll(@RequestBody Param param) {
        ResultVo resultVo = new ResultVo();
        Integer currentPage = Integer.valueOf(param.getCurrentPage());
        Integer pageSize = Integer.valueOf(param.getPageSize());
        System.out.println(param);
        if (("").equals(currentPage) || currentPage == 0) {
            currentPage = 1;
        }
        if (("").equals(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        if (Objects.isNull(param.getQueryString()) || ("").equals(param.getQueryString())) {
            IPage<TCheckgroup> page = new Page<>(currentPage, pageSize);
            resultVo.setData(tCheckgroupService.page(page));
            return resultVo;
        } else {
            IPage<TCheckgroup> page = new Page<>(currentPage, pageSize);
            Wrapper<TCheckgroup> qw = Wrappers.lambdaQuery(TCheckgroup.class)
                    .eq(TCheckgroup::getCode, param.getQueryString())
                    .or().eq(TCheckgroup::getName, param.getQueryString());
            TCheckgroup one = tCheckgroupService.getOne(qw);
            long count = tCheckgroupService.count(qw);
            System.out.println(count);
            System.out.println(count);
            if (count < 1) {
                IPage<TCheckgroup> page1 = new Page<>(currentPage, pageSize);
                resultVo.setData(tCheckgroupService.page(page1));
                resultVo.setCode(403);
                resultVo.setErrorMsg("项目编码或项目名称不存在");
                return resultVo;
            }
            page.setRecords(Arrays.asList(one));
            page.setCurrent(count);
            page.setSize(pageSize);
            page.setPages(currentPage);
            page.setTotal(count);

            resultVo.setData(page);
        }
        return resultVo;
    }

    @Transactional
    @PostMapping("/add.do")
    @ApiOperation(value = "检查组的添加")
    public ResultVo addTCheckitem(String checkitemIds, @RequestBody TCheckgroup group) {
        ResultVo resultVo = new ResultVo();
        String[] split;
        for (TCheckgroup tCheckgroup : tCheckgroupService.list()) {
            if (tCheckgroup.getCode().equals(group.getCode())) {
                resultVo.setCode(403);
                resultVo.setErrorMsg("编码重复修改失败");
                return resultVo;
            }
        }
        if (checkitemIds != null && checkitemIds.length() != 0) {
            split = checkitemIds.split(",");
            tCheckgroupService.save(group);
            Wrapper<TCheckgroup> qw = Wrappers.lambdaQuery(TCheckgroup.class)
                    .eq(TCheckgroup::getCode, group.getCode());
            TCheckgroup one = tCheckgroupService.getOne(qw);
            for (String s : split) {
                TCheckgroupCheckitem checkitem = new TCheckgroupCheckitem(one.getId().toString(), s);
                service.save(checkitem);
            }
            resultVo.setErrorMsg("添加成功");
        } else {
            resultVo.setErrorMsg("请选择项目编码");
            resultVo.setCode(403);
        }
        return resultVo;
    }


    @Transactional
    @PostMapping("/edit.do")
    @ApiOperation(value = "检查组的修改")
    public ResultVo updateTCheckitem(String checkitemIds, @RequestBody TCheckgroup group) {
        ResultVo resultVo = new ResultVo();
        String[] split = checkitemIds.split(",");
        TCheckgroup tCheckgroup = tCheckgroupService.getById(group.getId());
        Wrapper<TCheckgroup> wq = Wrappers.lambdaQuery(TCheckgroup.class)
                .ne(TCheckgroup::getCode, tCheckgroup.getCode());
        Wrapper<TCheckgroupCheckitem> qw = Wrappers.lambdaQuery(TCheckgroupCheckitem.class)
                .eq(TCheckgroupCheckitem::getCheckgroupId, group.getId());
        for (TCheckgroup tcheckgroup : tCheckgroupService.list(wq)) {
            if (tcheckgroup.getCode().equals(group.getCode())) {
                resultVo.setCode(403);
                resultVo.setErrorMsg("编码重复修改失败");
                return resultVo;
            }
        }
        if (checkitemIds != null && checkitemIds.length() != 0) {
            service.remove(qw);
            for (String s : split) {
                service.save(new TCheckgroupCheckitem(group.getId().toString(), s));
            }
        }
        Wrapper<TCheckgroup> qq=Wrappers.lambdaQuery(TCheckgroup.class)
                .eq(TCheckgroup::getId,group.getId());
        tCheckgroupService.update(group,qq);
        resultVo.setErrorMsg("修改成功");
        return resultVo;
    }

    @GetMapping("/findById.do")
    public ResultVo getById(String id) {
        System.out.println(id);
        ResultVo resultVo = new ResultVo();
        TCheckgroup tCheckgroup = tCheckgroupService.getById(id);
        if (tCheckgroup != null) {
            resultVo.setData(tCheckgroup);
        } else {
            resultVo.setErrorMsg("该项目不存在");
        }
        return resultVo;
    }

    @Transactional
    @GetMapping("/delete.do")
    @ApiOperation(value = "检查组的删除", notes = "必须在套餐删除完后检查组才能删除")
    public ResultVo delete(String id) {
        ResultVo resultVo = new ResultVo();
        TCheckgroup byId = tCheckgroupService.getById(id);

        Wrapper<TSetmealCheckgroup> qw = Wrappers.lambdaQuery(TSetmealCheckgroup.class)
                .eq(TSetmealCheckgroup::getCheckgroupId, id);
        long count = tservice.count(qw);
        if (count == 0) {
            if (byId != null) {
                Wrapper<TCheckgroupCheckitem> tgc = Wrappers.lambdaQuery(TCheckgroupCheckitem.class)
                        .eq(TCheckgroupCheckitem::getCheckgroupId, id);

                service.remove(tgc);
                System.out.println("================================================================");
                tCheckgroupService.removeById(id);
                resultVo.setErrorMsg("删除成功");
            } else {
                resultVo.setErrorMsg("删除失败");
                resultVo.setCode(403);
            }
        } else {
            resultVo.setErrorMsg("请先删除套餐");
            resultVo.setCode(403);
        }
        return resultVo;
    }

    @GetMapping("/byid")
    public ResultVo getTcheckitem(String id){
        ResultVo resultVo =new ResultVo();
        Wrapper<TCheckgroupCheckitem> qw=Wrappers.lambdaQuery(TCheckgroupCheckitem.class)
                .eq(TCheckgroupCheckitem::getCheckgroupId,id);
        List<String> list=new ArrayList<>();
        for (TCheckgroupCheckitem tCheckgroupCheckitem : service.list(qw)) {
            list.add(tCheckgroupCheckitem.getCheckgroupId());
        }
        resultVo.setData(list);
        return resultVo;
    }
}
